package com.scalar.chiptrack.accountsetup.actions;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.accountsetup.forms.AccountsSetupForm;
import com.scalar.chiptrack.accountsetup.dao.AccountsSetupDAO;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.users.User;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * Created by IntelliJ IDEA.
 * User: Sudhakar
 * Date: Dec 18, 2007
 * Time: 4:41:50 PM
 */
public class AccountsSetupAction extends ChipTrackAction
{

    private static final String ACTION_NAME = "/accountsSetup.do";

    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception
    {

        HashMap errors = new HashMap();
        HashMap info = new HashMap();
        String action = request.getParameter("action");

        AccountsSetupForm accountsSetupForm = (AccountsSetupForm) form;
        HttpSession session = request.getSession();

        User user = (User) session.getAttribute(SessionKeys.USER_KEY);

        if (user == null)
        {
            session.setAttribute(SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME + "?" + request.getQueryString());
            return (mapping.findForward("login"));
        }

        if(action == null)
        {
            try
            {
                accountsSetupForm = AccountsSetupDAO.loadAccountsSetUpInfo();
                request.setAttribute("accountsSetupForm", accountsSetupForm);
            }
            catch(Exception e)
            {
                errors.put ( ERROR_INTERNAL_SERVER_ERROR, "Error in loading Accounts Setup" );
            }

            return mapping.findForward( "success" );
        }
        if("save".equalsIgnoreCase( action ))
        {
            
            DBTransaction dbTransaction = null;
            boolean success             = false;

            try
            {
                dbTransaction = DBTransaction.getInstance();
                dbTransaction.begin();

                AccountsSetupDAO.saveAccountsSetupInfo(dbTransaction, accountsSetupForm);
                success = true;

            }
            catch(Exception e)
            {
               errors.put ( ERROR_INTERNAL_SERVER_ERROR, "Error in saving Accounts Setup" );
            }
            finally
            {
                if( success == true)
                {
                    dbTransaction.commit();
                    info.put( "Accounts Setup ", "Saved Successfully" );
                    request.setAttribute( SessionKeys.INFO_MESSAGES, info );
                }
                else
                {
                    dbTransaction.rollback();
                }
            }
            return mapping.findForward( "success" );
        }
        return mapping.findForward( "success" );
    }

}
